#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
 #include <fcntl.h>  
#include <pwd.h>
#include <grp.h>

char file_type(mode_t mode)
{
	char r;

	switch (mode & S_IFMT) {
		case S_IFREG:
			r = '-';
			break;
		case S_IFDIR:
			r = 'd';
			break;
		case S_IFCHR:
			r = 'c';
			break;
		case S_IFBLK:
			r = 'b';
			break;
		case S_IFSOCK:
			r = 's';
			break;
		case S_IFLNK:
			r = 'l';
			break;
		case S_IFIFO:
			r = 'p';
			break;
		default:
			r = '?';
			break;
	}
	return r;
}

int file_mode(mode_t mode, char *buf, size_t size)
{
	int arr[] = {S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP, S_IROTH, S_IWOTH, S_IXOTH};
	int i;

	if(size < 10){
		return -1;
	}
	strncpy(buf, "rwxrwxrwx", size);
	for(i = 0; i < 9; i++){
		if(!(arr[i] & mode)){
			buf[i] = '-';
		}
	}
	return 0;
}

int file_nlink(nlink_t n)
{
	return n;
}

char *file_owner(uid_t uid)
{
	struct passwd *p = getpwuid(uid);
	if(p == NULL){
		return NULL;
	}
	return p->pw_name;
}


char *file_group(gid_t gid)
{
	struct group *g = getgrgid(gid);
	if(g == NULL)
		return NULL;
	return g->gr_name;
}



